package org.mybatis.generator.plugin.ext;

import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;

/**
 * @author 孤傲苍狼
 *
 */
public class BaseMapperGeneratorPlugin extends PluginAdapter {


    //BaseMapper所在的包
    private String baseMapperPack;

    private FullyQualifiedJavaType interfaceType;
    private String project;

    public boolean validate(List<String> warnings) {
        baseMapperPack = properties.getProperty("baseMapperPack");
        project = properties.getProperty("targetProject");
        return true;
    }

    /**
     * 生成dao
     */
    @Override
    public boolean clientGenerated(Interface interfaze,
                                   TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

        FullyQualifiedJavaType pkType = null;
        List<IntrospectedColumn> primaryKeyColumns = introspectedTable.getPrimaryKeyColumns();
        if (primaryKeyColumns.isEmpty()) {
            pkType = new FullyQualifiedJavaType("java.lang.String");
        } else {
            pkType = primaryKeyColumns.get(0).getFullyQualifiedJavaType();//TODO:默认不考虑联合主键的情况
            System.out.println("primaryKey Type:" + pkType);
        }

        /**
         * 主键
         */
        FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType("BaseMapper<"
                + introspectedTable.getBaseRecordType() + ","
                + introspectedTable.getExampleType() + ","
                + pkType + ">");
        FullyQualifiedJavaType imp = new FullyQualifiedJavaType(
                baseMapperPack+".BaseMapper");
        /**
         * 添加 extends MybatisBaseMapper
         */
        interfaze.addSuperInterface(fqjt);

        /**
         * 添加import my.mabatis.example.base.MybatisBaseMapper;
         */
        interfaze.addImportedType(imp);
        /**
         * 方法不需要
         */
        interfaze.getMethods().clear();
        interfaze.getAnnotations().clear();

        return true;
    }

   /* @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
        List<GeneratedJavaFile> files = new ArrayList<>();
        try {
            interfaceType = new FullyQualifiedJavaType(baseMapperPack+"."+"BaseMapper");
            Interface interfaceBaseMapper = new Interface(interfaceType);
            // 导入必要的类
            addImport(interfaceBaseMapper);
            // 接口
            addInterface(interfaceBaseMapper,files);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return files;
    }

    *//**
     * 添加需要导入的包
     * @param interfaces
     *//*
    private void addImport(Interface interfaces) {
        interfaces.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Param"));
        interfaces.addImportedType(new FullyQualifiedJavaType("java.io.Serializable"));
        interfaces.addImportedType(new FullyQualifiedJavaType("java.util.List"));
    }


    protected void addInterface(Interface interface1, List<GeneratedJavaFile> files) {
        interface1.setVisibility(JavaVisibility.PUBLIC);
        // 添加方法
        List<Method> methodList = addMethods();
        for(Method method:methodList){
            interface1.addMethod(method);
        }
        GeneratedJavaFile file = new GeneratedJavaFile(interface1, project, context.getJavaFormatter());
        file.getFormattedContent();
        files.add(file);
    }


    protected List<Method> addMethods() {
        List<Method> methodList = new ArrayList<>();
        Method method = new Method();
        method.setName("countByExample");
        method.setReturnType(FullyQualifiedJavaType.getIntInstance());
        method.addParameter(new Parameter(new FullyQualifiedJavaType("E"), "example"));
        method.setVisibility(JavaVisibility.PUBLIC);
        methodList.add(method);
        return methodList;
    }*/

}